package com.example.mvpdemo.simple3;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.mvpdemo.R;

import androidx.appcompat.app.AppCompatActivity;

public class Simple3Activity extends AppCompatActivity implements WeatherView_3 {

    /**
     * simple3
     * mvp
     * 优化：
     * 通过抽象父类实现绑定和解绑
     * 发现问题：
     * 抽象父类类型写死
     * 解决方案：
     * 方案一：泛型设计
     * 方案二：强制类型转换（每次都需要进行强制类型转换，很麻烦）
     * 这里我们使用泛型设计来解决这个问题 ——>
     * Simple4中进行改进
     *
     */

    private TextView mWeatherContent;
    private EditText mCitynameEdit;
    String cityname = "北京";
    WeatherPresenter_3 weatherPresenter_3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple3);

        mWeatherContent = findViewById(R.id.weather_content_tv);
        mCitynameEdit = findViewById(R.id.cityname_edit);
        mCitynameEdit.setText(cityname);
    }


    public void getWeatherData(View view) {

        cityname = mCitynameEdit.getText().toString().trim();
        weatherPresenter_3 = new WeatherPresenter_3();
        weatherPresenter_3.attachView(this);
        weatherPresenter_3.weeather(cityname);

    }

    @Override
    public void onWeatherGetSuccess(String result) {
        mWeatherContent.setText("查询结果：\n" + result);
    }

    @Override
    public void onWeatherGetFail(String result) {
        mWeatherContent.setText("查询结果：\n" + result);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (weatherPresenter_3 != null) {
            weatherPresenter_3.detachView();
        }
    }


}